package com.briup.store.web.controller;

import com.briup.common.response.Result;
import com.briup.common.utils.UserContext;
import com.briup.store.service.ShopcarService;
import com.briup.store.domain.vo.ShopCarGetListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shopCars")
public class ShopCarController {

    @Autowired
    private ShopcarService shopcarService;

    /**
     * 获取购物车数量
     */
    @GetMapping
    public Result getCount(){
        Integer count = shopcarService.getCount(UserContext.getUser());
        return Result.success(count);
    }

    /**
     * 将商品填加到购物车
     */
    @PostMapping("/{goodsId}/{num}")
    public Result addShopCar(@PathVariable Integer goodsId,@PathVariable Integer num){
        shopcarService.addShopCar(goodsId,num);
        return Result.success();
    }

    /**
     * 查询某用户下购物车商品信息
     */
    @GetMapping("/getList")
    public Result getList(){
        List<ShopCarGetListVO> shopCarGetList =  shopcarService.getList();
        return Result.success(shopCarGetList);
    }

    /**
     * 修改购物车商品数量
     */
    @PutMapping("/{id}/{num}")
    public Result updateNum(@PathVariable Integer id,@PathVariable Integer num){
        System.out.println("in updateNum, id: " + id + ", num: " + num);
        shopcarService.updateNum(id,num);
        return Result.success();
    }

    /**
     * 删除与批量删除购物车信息
     */
    @DeleteMapping
    public Result batchDelete(@RequestBody List<Integer> ids){
        shopcarService.batchDelete(ids);
        return Result.success();
    }

    // 服务调用远程删除
    @DeleteMapping("/remoteDelete")
    public void deleteCartItemByIds(@RequestParam("goodIds") List<Integer> goodIds){
        // TODO 写死当前用户
        shopcarService.removeByUserIdAndGoodsId(goodIds);
    }
}
